package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.module.basic.dao.TDeptDao;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.half.dao.BasicDao;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.InventoryCheckDao;
import com.zhongwang.mms.module.half.model.HInBillModel;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.util.BigDecimalUtils;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:获得字典表信息
 * @author: th
 * @time: 2020/4/25 8:55
 */
@Service
public class HBasicService {

    @Autowired
    private TDeptDao tDeptDao;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private BasicDao basicDao;
    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private InventoryCheckDao inventoryCheckDao;

    /**
     * 获取部门
     * @return
     */
    public List<TDept> getDept() {

        return basicDao.getDept();
    }

    /**
     * 获取外协件库房
     * @return
     */
    public List<TWarehouseInfo> getWarInfo() {

        return basicDao.getWarInfo();
    }

    /**
     * 获取制作件库位
     * @return
     */
    public List<TWarehouseInfo> getFinishWarInfo(){
        return basicDao.getFinishWarInfo();
    }

    /**
     * 根据库房名获取ID
     * @param WhiName
     * @return
     */
    public Integer getWarIdByName(String WhiName){

        return basicDao.getWarIdByName(WhiName);
    }

    /**
     * 校验出库单数据可用性
     *
     * @param hInBillModel
     * @return
     */
    public String checkOutValidate(HInBillModel hInBillModel, String msg) {

        // 循环判断可用在库库存量是否充足（提示内容 可用在库库存量不足，保存失败！）
        HInventory inventory;
        HOrderDetailModel orderDetailModel;
        List<HOrderDetailModel> orderDetailModelList = new ArrayList<>();

        for (int i = 0; i < hInBillModel.getInsertData().size(); i++) {
            if (hInBillModel.getInsertData().get(i).getItyId() == null) {
                continue;
            }

            // 同张单据多条数据相同时，需要将出库数量累加之后再进行库存是否充足校验
            boolean sameFlg = false;
            for (int j = 0; j < orderDetailModelList.size(); j++) {
                if (sameFlg) {
                    break;
                }
                Integer insertDetailId = hInBillModel.getInsertData().get(i).getItyId();
                Integer outDetailId = orderDetailModelList.get(j).getItyId();
                if (insertDetailId.intValue() == outDetailId.intValue()) {
                    orderDetailModelList.get(j).setOdaNum(BigDecimalUtils.add(orderDetailModelList.get(j).getOdaNum(), hInBillModel.getInsertData().get(i).getOdaAvaNum()));
                    sameFlg = true;
                }
            }
            if (!sameFlg) {
                orderDetailModel = new HOrderDetailModel();
                orderDetailModel.setItyId(hInBillModel.getInsertData().get(i).getItyId());
                orderDetailModel.setOdaNum(hInBillModel.getInsertData().get(i).getOdaAvaNum());
                orderDetailModelList.add(orderDetailModel);
            }
        }

        for (var i = 0; i < orderDetailModelList.size(); i++) {

            // 取得原料在库明细信息，与实际出库量进行比对，判断库存是否充足
            inventory = hInventoryDao.selectAllById(orderDetailModelList.get(i).getItyId());

            double a = inventory.getItyAvaNum();
            double c = orderDetailModelList.get(i).getOdaNum();
            if (a < c) {
                if(hInBillModel.getInsertData().get(i).getOdaMatType()!= null && hInBillModel.getInsertData().get(i).getOdaMatType() == 4){
                    msg = msg + "<br>" + hInBillModel.getInsertData().get(i).getOdaMatName() + " " + hInBillModel.getInsertData().get(i).getOdaTexture() + " " + hInBillModel.getInsertData().get(i).getOdaSpec()  ;
                }else {
                    msg = msg + "<br>" + hInBillModel.getInsertData().get(i).getOdaMatName();
                }
            }
        }
        return msg;
    }

    /**
     * 获取盘点计划编号
     * @return
     */
    public String generateIPCOrderCode() {
        String newGenerateIPCOrderCode = "";
        //获取最后编码
        String lastIPCOrderCode = inventoryCheckDao.findLastIPCOrderCode();
        //获取当前时间简码
        String datetimeCode = new SimpleDateFormat("yyMMdd").format(new Date());
        Integer sn = 0;
        if (null != lastIPCOrderCode && lastIPCOrderCode.length() > 0) {
            //获取外购件库最新最后的入库单号
            sn = Integer.parseInt(lastIPCOrderCode.substring(9));
        }
        if (null == lastIPCOrderCode || lastIPCOrderCode.length() == 0) {
            newGenerateIPCOrderCode = "IPC" + new SimpleDateFormat("yyMMdd").format(new Date()) + "001";
        } else if (!lastIPCOrderCode.substring(3, 9).equals(datetimeCode)) {
            newGenerateIPCOrderCode = "IPC" + new SimpleDateFormat("yyMMdd").format(new Date()) + "001";
        } else {
            newGenerateIPCOrderCode = "IPC" + new SimpleDateFormat("yyMMdd").format(new Date()) + String.format("%03d", (sn + 1));
        }
        return newGenerateIPCOrderCode;
    }

    public TDept getDeptNameById(Integer odrDept) {
        return tDeptDao.selectByPrimaryKey(odrDept);
    }
}
